/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.noraxidium.model;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.tiled.TiledMap;
import org.pleasantnightmare.noraxidium.collision.RectBound;
import org.pleasantnightmare.noraxidium.math.GameVector2f;
import org.pleasantnightmare.noraxidium.support.MapSupport;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Wrapper around TiledMap, designed to cache additional gameplay information.
 *
 * @author deus
 * @version 1.0
 * @since Jul 9, 2010 11:23:27 AM
 */
public class LevelMap implements Updated {
  private String filename;
  private TiledMap map;
  private GameVector2f startPosition;

  private List<RectBound> solidCollision;
  private Set<GameObject> objects;

  public LevelMap(String filename, TiledMap map) {
    this.filename = filename;
    this.map = map;
    startPosition = new GameVector2f();
    solidCollision = new LinkedList<RectBound>();
    objects = new HashSet<GameObject>();
  }

  public void addGameObject(GameObject gameObject) {
    objects.add(gameObject);
    gameObject.setLevelMap(this);
  }

  public void removeGameObject(GameObject gameObject) {
    objects.remove(gameObject);
    gameObject.setLevelMap(null);
  }

  public Set<GameObject> getObjects() {
    return objects;
  }

  public String getFilename() {
    return filename;
  }

  public TiledMap getMap() {
    return map;
  }

  public void setStartPosition(GameVector2f startPosition) {
    this.startPosition.set(startPosition);
  }

  public GameVector2f getStartPosition() {
    return startPosition;
  }

  @Override public String toString() {
    return "LevelMap{" +
        "filename='" + filename + '\'' +
        '}';
  }

  public void renderBackLayer(int x, int y) {
    map.render(x, y, MapSupport.LAYER_REPRESENTATION_BACK);
  }

  public void renderFrontLayer(int x, int y) {
    map.render(x, y, MapSupport.LAYER_REPRESENTATION_FRONT);
  }

  public void setSolidCollision(List<RectBound> solidCollision) {
    this.solidCollision.clear();
    this.solidCollision.addAll(solidCollision);
  }

  public List<RectBound> getSolidCollision() {
    return solidCollision;
  }

  @Override public void update(int delta) {
    updateMapObjects(delta);
  }

  private void updateMapObjects(int delta) {
    for (GameObject gameObject : objects)
      gameObject.update(delta);
  }

  public void render(int x, int y, Graphics g) {
    renderBackLayer(0, 0);
    for (GameObject object : objects)
      object.render(g);
    renderFrontLayer(0, 0);
  }
}
